import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ConfigService } from './config.service';
import { News } from '../model/news';

@Injectable()
export class NewsService {

  constructor(private http:HttpClient,
    private config:ConfigService) { }

    private getNewsListUrl=this.config.host+"/manage/getNewsList";
    getNewsList(){
    
      return this.http.get(this.getNewsListUrl).toPromise();
    }

    private addNewsUrl=this.config.host+"/manage/addNews";
    addNews(news:News){
      let p={
        title:news.title,
        author:news.author,
        content:news.content,
        px:news.px
      }
      return this.http.post(this.addNewsUrl,p).toPromise();
    }

    private deleteNewsUrl=this.config.host+"/manage/deleteNews";
    deleteNews(id:string){
      let p={
        "newid":id
      }
      return this.http.post(this.deleteNewsUrl,p).toPromise();
    }

    private geSingleNewsUrl=this.config.host+"/manage/getSingleNews";
    getSingleNews(id:string){
      let p={
        "newid": id
      }
      return this.http.post(this.geSingleNewsUrl,p).toPromise();

    }

    private updateNewsUrl=this.config.host+"/manage/updateNews";
    updateNews(news:News){
      let p= {
        "newid": news.newid,
        "title": news.title,
        "pbdate":news.pbdate,
        "author":news.author,
        "content": news.content,
        "px": news.px
    }
    return this.http.post(this.updateNewsUrl,p).toPromise();

    }

    private uploadNewCoverPicUrl=this.config.host+"/manage/uploadNewCoverpic";
    uploadNewCoverPic(newsid:string,file:string){

      const httpOptions = {
        headers: new HttpHeaders({
          'newsid':  newsid
        })
      };

      const uploadData = new FormData();
      uploadData.append('file', file);

      return this.http.post(this.uploadNewCoverPicUrl,uploadData,httpOptions).toPromise();
    }
}
